// PatentController.java
package com.ts.controller.achievement;

import com.ts.constant.AchievementConstant;
import com.ts.dto.PatentDTO;
import com.ts.dto.PageDTO;
import com.ts.entity.Patent;
import com.ts.entity.TsAchievement;
import com.ts.result.PageResult;
import com.ts.result.Result;
import com.ts.service.achievement.PatentService;
import com.ts.service.achievement.TsAchievementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 专利相关接口 - 专利相关成果的增删改查
 */
@RestController
@RequestMapping("/achievement/patent")
@Tag(name = "专利相关接口", description = "专利的增删改查及分页查询")
@Slf4j
public class PatentController {

    @Autowired
    private PatentService patentService;
    @Autowired
    private TsAchievementService tsAchievementService;

    /**
     * 新增专利
     */
    @Operation(summary = "新增专利", description = "提交专利成果信息")
    @PostMapping
    public Result save(
            @Parameter(description = "专利信息DTO", required = true)
            @RequestBody PatentDTO patentDTO
    ) {
        log.info("新增专利，参数：{}", patentDTO);
        patentService.save(patentDTO);


        Long PatentId = patentDTO.getId();
        log.info("论文新增成功，ID：{}", PatentId);

        // 2. 构建成果所需参数（从DTO中提取或构建）
        String achievementName = patentDTO.getPatentName(); // 成果名称复用论文名称
        // 构建成果描述（使用论文特有字段）
        String type = AchievementConstant.PATENT;
        String description = String.format("专利《%s》，获批时间《%s》，作者：%s",
                patentDTO.getPatentName(),
                patentDTO.getApprovalTime(),
                patentDTO.getAuthor());
        // Integer level = paperDTO.getAchievementSourceLevel(); // 假设DTO中有级别字段，或根据业务默认赋值
        Integer level = 1;// 假设DTO中有级别字段，或根据业务默认赋值
        Integer createdBy =  1;
        // 3. 同步新增关联的成果记录（论文类型）
        TsAchievement achievement = tsAchievementService.addAchievement(
                achievementName,
                type,
                description,
                level,
                createdBy,
                PatentId
        );
        log.info("同步创建成果成功，成果ID：{}", achievement.getId());

        return Result.success();
    }

    /**
     * 根据ID删除专利
     */
    @Operation(summary = "删除专利", description = "根据ID删除指定专利")
    @DeleteMapping("/{id}")
    public Result delete(
            @Parameter(description = "专利ID", required = true)
            @PathVariable Long id
    ) {
        log.info("删除专利，ID：{}", id);
        patentService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改专利
     */
    @Operation(summary = "修改专利", description = "更新专利成果信息")
    @PutMapping
    public Result update(
            @Parameter(description = "专利信息DTO（含ID）", required = true)
            @RequestBody PatentDTO patentDTO
    ) {
        log.info("修改专利，参数：{}", patentDTO);
        patentService.updateById(patentDTO);
        return Result.success();
    }

    /**
     * 根据ID查询专利
     */
    @Operation(summary = "查询单个专利", description = "根据ID获取专利详情")
    @GetMapping("/{id}")
    public Result<Patent> getById(
            @Parameter(description = "专利ID", required = true)
            @PathVariable Long id
    ) {
        log.info("查询专利，ID：{}", id);
        Patent patent = patentService.getById(id);
        return Result.success(patent);
    }

    /**
     * 分页查询专利列表
     */
    @Operation(summary = "分页查询", description = "专利的分页查询")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(
            @Parameter(description = "分页查询参数DTO")
                    PageDTO dto
    ) {
        log.info("专利分页查询，参数：{}", dto);
        PageResult pageResult = patentService.pageQuery(dto);
        return Result.success(pageResult);
    }
}